package com.nineclock.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NCException;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.common.utils.CurrentUserHolder;
import com.nineclock.dto.SysCompanyUserDTO;
import com.nineclock.dto.SysRoleDTO;
import com.nineclock.pojo.SysCompanyUser;
import com.nineclock.pojo.SysDepartment;
import com.nineclock.pojo.SysRole;
import com.nineclock.system.mapper.SysCompanyUserMapper;
import com.nineclock.system.mapper.SysDepartmentMapper;
import com.nineclock.system.mapper.SysRoleMapper;
import com.nineclock.system.service.SysCompanyUserService;
import com.nineclock.system.service.SysOrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
@Service
@Transactional
public class SysCompanyUserServiceImpl implements SysCompanyUserService {
    @Autowired
    private SysCompanyUserMapper sysCompanyUserMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Override
    public List<SysCompanyUserDTO> findCompanyUserByUserIdAndCompanyId(Long userId, Long companyId) {
        if (userId == null && companyId == null) {
            throw new NCException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        // 封装查询参数
        LambdaQueryWrapper<SysCompanyUser> queryWrapper = new LambdaQueryWrapper<>();
        if (userId != null) {
            queryWrapper.eq(SysCompanyUser::getUserId, userId);
        }
        if (companyId != null) {
            queryWrapper.eq(SysCompanyUser::getCompanyId, companyId);
        }
        List<SysCompanyUser> companyUserList = sysCompanyUserMapper.selectList(queryWrapper);
        if (companyUserList == null) {
            throw new NCException(ResponseEnum.USER_NOT_FOUND);
        }
        List<SysCompanyUserDTO> companyUserDTOS = BeanHelper.copyWithCollection(companyUserList, SysCompanyUserDTO.class);
        // 查询员工所对应的角色信息
        companyUserDTOS.forEach(sysCompanyUserDTO -> {
            List<SysRole> roleList = sysRoleMapper.findRoleByUserIdAndCompanyId(sysCompanyUserDTO.getUserId(), sysCompanyUserDTO.getCompanyId());
            if (roleList == null) {
                throw new NCException(ResponseEnum.ROLE_NOT_FOUND);
            }
            sysCompanyUserDTO.setRoles(BeanHelper.copyWithCollection(roleList, SysRoleDTO.class));
        });
        return companyUserDTOS;
    }

    // 根据id查询员工信息
    @Override
    public SysCompanyUserDTO findCompanyUserById(Long companyUserId) {
        if (companyUserId == null) {
            throw new NCException(ResponseEnum.MESSAGE_PARAMS_LOST);
        }
        SysCompanyUser sysCompanyUser = sysCompanyUserMapper.selectById(companyUserId);
        if (sysCompanyUser == null) {
            throw new NCException(ResponseEnum.USER_NOT_FOUND);
        }
        return BeanHelper.copyProperties(sysCompanyUser, SysCompanyUserDTO.class);
    }
// APP根据手机号获取员工信息
    @Override
    public SysCompanyUserDTO queryone(String mobile) {
        if (StrUtil.isNotEmpty(mobile)) {
            LambdaQueryWrapper<SysCompanyUser> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SysCompanyUser::getCompanyId, CurrentUserHolder.get().getCompanyId());
            queryWrapper.eq(SysCompanyUser::getMobile, mobile);
            SysCompanyUser sysCompanyUser = sysCompanyUserMapper.selectOne(queryWrapper);
            return BeanHelper.copyProperties(sysCompanyUser, SysCompanyUserDTO.class);
        }
        return null;
    }

    /**
     * 获取当前企业所有员工
     * @return
     */
    @Override
    public List<SysCompanyUserDTO> queryAllCompanyUsers() {
        Long companyId = CurrentUserHolder.get().getCompanyId();
        // 组装查询条件,当前企业,以及员工状态可用的所有
        LambdaQueryWrapper<SysCompanyUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysCompanyUser::getCompanyId, companyId);
        queryWrapper.eq(SysCompanyUser::getEnable, (short) 1);
        List<SysCompanyUser> sysCompanyUsers = sysCompanyUserMapper.selectList(queryWrapper);
        if (sysCompanyUsers != null) {
            return BeanHelper.copyWithCollection(sysCompanyUsers, SysCompanyUserDTO.class);
        }

        return null;
    }

    /**
     * 根据企业iD查询管理员信息
     * @param companyId
     * @return
     */
    @Override
    public SysCompanyUserDTO queryAdminByCompanyId(Long companyId) {
        SysCompanyUser companyUserAdmin = sysCompanyUserMapper.findCompanyUserAdmin(companyId);
        if (companyUserAdmin != null) {
            return BeanHelper.copyProperties(companyUserAdmin, SysCompanyUserDTO.class);
        }
        return null;
    }
}
